<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="关键字:" prop="keyword">
        <el-input
          size="mini"
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间:">
        <el-date-picker
          size="mini"
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="审核状态" prop="currentStep" key="currentStep" align="center" width="70"/>
      <el-table-column label="审核备注" prop="failedRemark" key="failedRemark" align="center" width="70"/>
      <el-table-column label="申报政策细则名称" prop="itemName" key="itemName" align="center" width="220"/>
      <el-table-column label="企业名称" prop="name" key="name" align="center" width="200"/>
      <el-table-column label="经办人" prop="operator" key="operator" align="center" width="80"/>
      <el-table-column label="联系方式" prop="tel" key="tel" align="center" width="100"/>
      <el-table-column label="银行名称" prop="bankName" key="bankName" align="center" width="200"/>
      <el-table-column label="银行账号（对公账号）" prop="bankAccount" key="bankAccount" align="center" width="200"/>
      <el-table-column label="持卡人信息（对公账号）" prop="cardHolding" key="cardHolding" align="center" width="170"/>
      <el-table-column label="承诺书" key="commitment" align="center" width="80">
        <template slot-scope="scope">
          <image-preview class="list-img" :src="scope.row.commitment"/>
        </template>
      </el-table-column>
      <el-table-column label="经营活动图" key="operateImg" align="center" width="80">
        <template slot-scope="scope">
          <image-preview class="list-img" :src="scope.row.operateImg"/>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="applyTime" min-width="100">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" key="action" align="center" width="100">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.primaryStatus === '0'"
            type="text"
            size="mini"
            v-hasPermi="['system:shPolicyAudit:edit']"
            @click="handleEdit(scope.row)">审核</el-button>
          <el-button
            v-if="scope.row.currentStep === '兑现'"
            type="text"
            size="mini"
            v-hasPermi="['system:shPolicyAudit:fulfill']"
            @click="handleFulfill(scope.row)">兑现</el-button>
          <el-button
            type="text"
            plain
            size="mini"
            @click="handleExport(scope.row)"
          >导出</el-button>
          <el-button
            type="text"
            size="mini"
            v-hasPermi="['system:shPolicyAudit:remove']"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="120px">
        <el-form-item v-if="auditForm.currentStep === '初审'" label="是否诚信经营:" prop="isHonest">
          <el-radio-group v-model="auditForm.isHonest">
            <el-radio label="0">否</el-radio>
            <el-radio label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="auditForm.currentStep === '初审'" label="是否在库:" prop="isExist">
          <el-radio-group v-model="auditForm.isExist">
            <el-radio label="0">否</el-radio>
            <el-radio label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="auditForm.currentStep === '初审' && auditForm.isExist === '0'" label="退库时间:" prop="quitTime">
          <el-date-picker
            v-model="auditForm.quitTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width: 100%;"/>
        </el-form-item>
        <el-form-item v-if="auditForm.currentStep === '初审'" label="审核结果:" prop="status">
          <el-radio-group v-model="auditForm.status">
            <el-radio label="1">同意拨付</el-radio>
            <el-radio label="2">补充资料退回修改</el-radio>
            <el-radio label="3">企业不符合申报条件不拨付</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="兑现" :visible.sync="fulfillOpen" width="600px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" label-width="120px">
        <el-form-item label="支付凭证:" prop="evidence">
          <ImageUpload v-model="auditForm.evidence" :limit="3"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="fulfillOpen = false">取消</el-button>
        <el-button type="primary" @click="submitFulfill">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { pageList,audit,fulfill,remove } from '@/api/triones/shPolicyAudit'
export default {
  name: 'index',
  data() {
    return {
      loading: false,
      showSearch: true,
      dataList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined,
        beginTime: undefined,
        endTime: undefined
      },
      open: false,
      title: '',
      auditForm: {},
      auditRules: {
        status: [
          { required: true, message: '请选择审核结果', trigger: 'blur' }
        ]
      },
      fulfillOpen: false,
      dateRange: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      if (this.dateRange) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }
      pageList(this.queryParams).then(res => {
        this.dataList = res.rows;
        this.total = res.total;
        this.loading = false;
      })
    },
    handleEdit(row) {
      this.reset();
      const rowInfo = row;
      this.auditForm = {
        id: rowInfo.id,
        currentStep: rowInfo.currentStep
      };
      this.title = '审核';
      this.open = true;
    },
    reset() {
      this.auditForm = {
        id: undefined,
        status: undefined,
        failedRemark: undefined,
        currentStep: undefined,
        isExist: undefined,
        quitTime: undefined,
        isHonest: undefined,
        evidence: undefined
      }
      this.resetForm("auditForm");
    },
    submitForm() {
      this.$refs["auditForm"].validate(valid => {
        if (this.auditForm.status === '2') {
          this.auditForm.failedRemark = "补充资料退回修改";
        }
        if (this.auditForm.status === '3') {
          this.auditForm.status = '2';
          this.auditForm.failedRemark = "企业不符合申报条件不拨付";
        }
        audit(this.auditForm).then(response => {
          this.$modal.msgSuccess("审核成功");
          this.open = false;
          this.getList();
        });
      })
    },
    handleFulfill(row) {
      this.reset();
      const rowInfo = row;
      this.auditForm = {
        id: rowInfo.id
      };
      this.fulfillOpen = true;
    },
    submitFulfill() {
      fulfill(this.auditForm).then(response => {
        this.$modal.msgSuccess("成功兑现");
        this.fulfillOpen = false;
        this.getList();
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleExport(row) {
      const auditIds = row.id || this.ids;
      if (auditIds === null || auditIds.length === 0) {
        return this.$message.error("请勾选要导出的数据")
      }
      this.download('system/declareAudit/export/' + auditIds, {

      }, `audit_${new Date().getTime()}.xlsx`)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除企业名称为"' + row.name + '"对政策细则名称为"'+ row.itemName + '"的申报记录数据项？').then(() => {
        this.loading = true;
        return remove(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    }
  }
}
</script>
<style scoped lang="scss">

</style>
